package com.example.myapplication.activity;

import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.bean.PersonBean;
import com.example.myapplication.service.AService_;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterTextChange;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.BeforeTextChange;
import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.FocusChange;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.LongClick;
import org.androidannotations.annotations.SystemService;
import org.androidannotations.annotations.TextChange;
import org.androidannotations.annotations.Touch;
import org.androidannotations.annotations.ViewById;

//@Fullscreen
@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    private static int lan = 0;

    @SystemService
    ConnectivityManager connectivityManager;

    @Bean
    PersonBean personBean;

    @ViewById
    TextView textView;

    @ViewById
    Button btn_A,btn_B,btn_C,btn_D,btn_E,btn_F,
            btn_G,btn_H,btn_I,btn_J,btn_K;

    @ViewById
    EditText editText;

    @ViewById
    RadioButton rb_A, rb_B;

    @Extra("key_value")
    String message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    // 点击事件注入
    @Click({R.id.btn_A,R.id.btn_B,R.id.btn_C,R.id.btn_D,R.id.btn_E,
            R.id.btn_F,R.id.btn_I,R.id.btn_J,R.id.btn_K})
    public void clickButton(View view) {
        switch (view.getId()) {
            case R.id.btn_A:
//                Intent intentA = new Intent(this, SecondActivity_.class);
                Intent intentA = MainActivity_.intent(this).message("Hello World!").get();
                intentA.setClass(this, SecondActivity_.class);
                this.startActivity(intentA);
//                finish();
                break;
            case R.id.btn_B:
                if (lan == 0) { // 英文转中文
                    textView.setText("你好，世界！");
                    lan = 1;
                } else { // 中文转英文
                    textView.setText("Hello World!");
                    lan = 0;
                }
                break;
            case R.id.btn_C:
                personBean.setUsername("root");
                personBean.setPassword("123456");
                Toast.makeText(this, "对象信息为："+personBean.toString(),
                        Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_D:
                Intent intentB = new Intent(this, CFragmentActivity_.class);
                startActivity(intentB);
                break;
            case R.id.btn_E:
                AService_.intent(this).start();
                break;
            case R.id.btn_F:
                // 获取NetworkInfo对象
                NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
                if (networkInfo != null && networkInfo.length > 0) {
                    for (int i=0; i < networkInfo.length; i++) {
                        System.out.println(i + "===状态===" + networkInfo[i].getState());
                        System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
                        // 判断当前网络状态是否为连接状态
                        if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                            Toast.makeText(this, "当前有网络...",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, "当前无网络...",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                }
                break;
            case R.id.btn_G:
//                Log.d("@Click", "当前按钮被点击");
                break;
            case R.id.btn_H:
                Log.d("@Click", "当前按钮被点击");
                break;
            case R.id.btn_I:
                Intent intentC = new Intent(this, ItemClickActivity_.class);
                this.startActivity(intentC);
                break;
            case R.id.btn_J:
                Intent intentD = new Intent(this, ThreadTestActivity_.class);
                this.startActivity(intentD);
                break;
            case R.id.btn_K:
                Intent intentE = new Intent(this, ListDemoActiviy_.class);
                this.startActivity(intentE);
                break;
        }
    }

    // 触摸事件注入
    @Touch(R.id.btn_G)
    public void touchButton(View view) {
//        btn_G.setBackgroundColor(getResources().getColor(R.color.color05));
        Log.d("@Touch", "当前按钮被触摸了");
    }

    // 点击事件注入
    @LongClick(R.id.btn_H)
    public void longClickButton(View view) {
        Log.d("@LongClick", "当前按钮被长按点击了");
    }

    // EditView 文本变化事件注入
    @BeforeTextChange(R.id.editText)
    public void beforeEd(CharSequence s, int start, int count, int after) {
        Log.d("@BeforeTextChange", "beforeEd:" + s);
    }
    @TextChange(R.id.editText)
    public void textEd(CharSequence s, int start, int before, int count) {
        Log.d("@TextChange", "textEd:" + s);
    }
    @AfterTextChange(R.id.editText)
    public void afterEd(Editable s) {
        Log.d("@AfterTextChange", "afterEd:" + s);
    }

    // EditView 焦点变化事件注入
    @FocusChange(R.id.editText)
    public void focusChange(View view, boolean hasFocus) {
        Log.d("@FocusChange", "当前editText焦点发生变化，状态为" + hasFocus);
    }

    // Radio 控件选择事件注入
    @CheckedChange({R.id.rb_A,R.id.rb_B})
    public void radioChecked(CompoundButton view, boolean isChecked) {
        if (view.getId() == R.id.rb_A) {
            Log.d("@CheckedChange", "按钮a的选中状态为" + isChecked);
        } else {
            Log.d("@CheckedChange", "按钮b的选中状态为" + isChecked);
        }
    }

    @AfterInject
    public void showAfterInjectThing() {
        Log.d("@AfterInject", "showAfterInjectThing: ...");
    }

    @AfterViews
    public void initViews() {
        textView.setText("已注入...");
    }
}
